package br.unicentro.lynx.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.unicentro.lynx.model.factory.ConnectionFactory;
import br.unicentro.lynx.model.pojo.Exemplar;

public class ExemplarDao {
	private String sql;
	private Connection connection;
	private PreparedStatement statement;
	private ResultSet result;

	public void insert(Exemplar exemplar) {
		open();
		this.sql = "INSERT INTO Exemplares (status_exemplar, id_book_exemplar, id_bookcase_exemplar) VALUES (?, ?, ?)";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setBoolean(1, exemplar.getStatusExemplar());
			this.statement.setInt(2, exemplar.getIdBookExemplar());
			this.statement.setInt(3, exemplar.getIdBookcaseExemplar());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void update(Exemplar exemplar) {
		open();
		this.sql = "UPDATE Exemplares SET status_exemplar = ?, id_book_exemplar = ?, id_bookcase_exemplar = ? WHERE id_exemplar = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setBoolean(1, exemplar.getStatusExemplar());
			this.statement.setInt(2, exemplar.getIdBookExemplar());
			this.statement.setInt(3, exemplar.getIdBookcaseExemplar());
			this.statement.setInt(4, exemplar.getIdExemplar());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void delete(Exemplar exemplar) {
		open();
		this.sql = "DELETE FROM Exemplares WHERE id_exemplar = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, exemplar.getIdExemplar());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public Exemplar search(Integer id_exemplar) {
		open();
		this.sql = "SELECT * FROM Exemplares WHERE id_exemplar = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, id_exemplar);
			this.result = this.statement.executeQuery();
			if (this.result.next()) {
				return new Exemplar(result.getInt("id_exemplar"),
						result.getBoolean("status_exemplar"),
						result.getInt("id_book_exemplar"),
						result.getInt("id_bookcase_exemplar"));
			} else {
				return null;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public ArrayList<Exemplar> list() {
		open();
		this.sql = "SELECT * FROM Exemplares";
		ArrayList<Exemplar> exemplares = new ArrayList<Exemplar>();
		try {
			this.statement = connection.prepareStatement(sql);
			this.result = this.statement.executeQuery();
			while (this.result.next()) {
				exemplares.add(new Exemplar(result.getInt("id_exemplar"),
						result.getBoolean("status_exemplar"), result
								.getInt("id_book_exemplar"), result
								.getInt("id_bookcase_exemplar")));
			}
			return exemplares;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				this.result.close();
				close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	private void open() {
		this.connection = ConnectionFactory.getConnection(
				ConnectionFactory.URL, ConnectionFactory.USER,
				ConnectionFactory.PASSWORD);
	}

	private void close() {
		try {
			this.statement.close();
			this.connection.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
